package jp.co.redfox.archclub.poker;

import java.util.Date;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
import java.util.Stack;

public class Deck {
    private Stack<Card> cardStack;
    private static final int ALL_CARD_NUMBERS = 52;
//    public static void main(String[] args) {
//        for (int cnt = 0; cnt < 1000; cnt++) {
//            new Deck().initialize();
//        }
//    }
    public void initialize() {
        cardStack = new Stack<>();
        EnumSet<Card> allCard = EnumSet.allOf(Card.class);
        Random random = new Random(new Date().getTime());
        Set<Integer> shuffleCards = new LinkedHashSet<>(ALL_CARD_NUMBERS);
//        int cnt = 0;
//        long start = System.currentTimeMillis();
        while (true) {
            int cardIndex = random.nextInt(ALL_CARD_NUMBERS);
            shuffleCards.add(cardIndex);
//            cnt++;
            if (shuffleCards.size() == ALL_CARD_NUMBERS)
                break;
        }
//        long end = System.currentTimeMillis();
//        System.out.println("cnt=" + cnt + "times set.   time=" + (end -start) + "msec");
        Object[] cardArray = allCard.toArray();
        for (Integer index : shuffleCards) {
            cardStack.push((Card) cardArray[index]);
        }
    }

    public Card dealCard() {
        return cardStack.pop();
    }
}
